package com.rongshu.view.penpal.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.discovery.activity.UserHomePageActivity;
import com.rongshu.view.penpal.adapter.PenpalMemberListAdapter;
import com.rongshu.view.penpal.bean.MemberDetails;
import com.rongshu.view.penpal.bean.MemberMsgResponse;
import com.rongshu.view.penpal.present.MemberManagerPresenter;
import com.rongshu.view.penpal.view.MemberManagerView;
import com.rongshu.view.penpal.view.PenpalMemberManagerListener;
import com.rongshu.view.userInfo.activity.DirectChatActivity;
import com.rongshu.view.userInfo.activity.PersonDetailsActivity;
import com.rongshu.view.userInfo.entity.DirectMessage;
import com.rongshu.widget.BanyanDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/6.
 * description：成员管理/查看
 */
public class MemberManagerActivity extends BaseActivity<MemberManagerPresenter> implements MemberManagerView {
    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.top_list)
    RecyclerView topList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshLayout;
    private List<MemberDetails> list;
    private int penpalwill_id = 0;
    private int memberOption = 0;//0 ：查看；1 ：操作
    private int pageIndex = 1;
    private int pageSize = 15;
    private PenpalMemberListAdapter adapter;
    private MemberMsgResponse response;

    @Override
    protected int initLayout() {
        return R.layout.activity_member_manager;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            penpalwill_id = intent.getIntExtra(Constant.PENPAL_ID, 0);
            memberOption = intent.getIntExtra(Constant.PENPAL_MEMBER_OPTION, 0);
        }
        headMiddle.setText(memberOption == 0 ? "查看成员" : "成员管理");
        refreshLayout.setEnableAutoLoadmore(false);
        list = new ArrayList<>();
        LinearLayoutManager groupManager = new LinearLayoutManager(this);
        topList.setLayoutManager(groupManager);
        adapter = new PenpalMemberListAdapter(this, list, memberOption);
        topList.setAdapter(adapter);
        adapter.setOnHeadClickListener(new AdapterListener() {
            @Override
            public void callBack(Object object) {
                if (object instanceof MemberDetails) {
                    MemberDetails details = (MemberDetails) object;
                    Intent intent = new Intent();
                    if (UserInfoUtils.getUserId() == details.getUser_id()) {
                        intent.setClass(MemberManagerActivity.this, PersonDetailsActivity.class);
                        startActivity(intent);
                    } else {
                        intent.setClass(MemberManagerActivity.this, UserHomePageActivity.class);
                        int userid = details.getUser_id();
                        intent.putExtra("UserId", userid);
                        startActivity(intent);
                    }
                }
            }
        });
        adapter.setAdapterListener(new PenpalMemberManagerListener() {

            @Override
            public void removePenpal(MemberDetails details) {
                presenter.memberOption(penpalwill_id, details.getUser_id(), MemberManagerView.REMOVE);
            }

            @Override
            public void toMember(MemberDetails details) {
                presenter.memberOption(penpalwill_id, details.getUser_id(), MemberManagerView.TO_MEMBER);
            }

            @Override
            public void toViceLeader(MemberDetails details) {
                presenter.memberOption(penpalwill_id, details.getUser_id(), MemberManagerView.TO_VICE_LEADER);
            }

            @Override
            public void toLeader(final MemberDetails details) {
                BanyanDialog.Builder builder = new BanyanDialog.Builder(MemberManagerActivity.this);
                builder.setTitle("是否移交会长");
                builder.setRight("确定");
                builder.setLeft("取消");
                final BanyanDialog dialog = builder.create();
                builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
                    @Override
                    public void leftClickListener() {
                        dialog.dismiss();
                    }

                    @Override
                    public void rightClickListener() {
                        presenter.memberOption(penpalwill_id, details.getUser_id(), MemberManagerView.TO_LEADER);
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }

            @Override
            public void privateLetter(MemberDetails details) {
                Intent intent = new Intent(MemberManagerActivity.this, DirectChatActivity.class);
                DirectMessage.ResponseBean bean = new DirectMessage.ResponseBean();
                bean.setBi_name(details.getBi_name());
                bean.setNewSend_userid(details.getUser_id());
                intent.putExtra(Constant.INTENT_DATA, bean);
                startActivity(intent);
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 1;
                refreshlayout.resetNoMoreData();
                presenter.getMemberMsg(penpalwill_id, pageIndex, pageSize);
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getMemberMsg(penpalwill_id, pageIndex, pageSize);
                refreshlayout.finishLoadmore(2000/*,false*/);//传入false表示刷新失败
            }
        });
    }

    @Override
    protected void initData() {
        setPresenter(new MemberManagerPresenter(this), this);
        presenter.getMemberMsg(penpalwill_id, pageIndex, pageSize);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }


    @OnClick(R.id.head_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void callBackMemberMsg(MemberMsgResponse response) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadmore();
        }
        if (response != null && response.getResponse() != null) {
            this.response = response;
            if (pageIndex == 1) {
                list.clear();
                adapter.setData(response.getResponse().getCurrentrank(),
                        response.getResponse().getManagementlist().size(),
                        response.getResponse().getOrdinaryCount());
                if (response.getResponse().getOrdinaryCount() > 0) {
                    response.getResponse().getOrdinarylist().get(0).setShowTag(true);
                }
                list.addAll(response.getResponse().getManagementlist());
            }
            list.addAll(response.getResponse().getOrdinarylist());
            adapter.notifyDataSetChanged();
            if (response.getResponse().getOrdinarylist().size() == 0) {
                refreshLayout.setLoadmoreFinished(true);
            }
        }

    }

    @Override
    public void memberOptionSuccess() {
        pageIndex = 1;
        refreshLayout.resetNoMoreData();
        presenter.getMemberMsg(penpalwill_id, pageIndex, pageSize);
    }
}
